{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_URL_MAG = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\n",
    "API_URL_INTER = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "response = requests.get(API_URL_MAG)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "if response.ok:\n",
    "    data = response.json()\n",
    "    # pprint(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "sql_head = \"INSERT INTO magazine_data(date, open, high, low, close, volume)\\n\" \\\n",
    "            \"VALUES\\n\"\n",
    "dictlist = data['Time Series (1min)'].items()\n",
    "sql_query = \"\"\n",
    "for dic in dictlist:\n",
    "    date, stocks_prices = dic\n",
    "    stocks_values = list(stocks_prices.values())\n",
    "    if not sql_query:\n",
    "        sql_query = \"    ('{:s}', {:s}, {:s}, {:s}, {:s}, {:s});\\n\".format(date, stocks_values[0], stocks_values[1], stocks_values[2], stocks_values[3], stocks_values[4]) + sql_query\n",
    "    else:\n",
    "        sql_query = \"    ('{:s}', {:s}, {:s}, {:s}, {:s}, {:s}),\\n\".format(date, stocks_values[0], stocks_values[1], stocks_values[2], stocks_values[3], stocks_values[4]) + sql_query\n",
    "sql_query = sql_head + sql_query\n",
    "\n",
    "file = open('sql_magazine_query.sql', 'w')\n",
    "file.write(sql_query)\n",
    "file.close()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "response = requests.get(API_URL_INTER)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "if response.ok:\n",
    "    data = response.json()\n",
    "    # pprint(data)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "sql_head = \"INSERT INTO inter_data(date, open, high, low, close, volume)\\n\" \\\n",
    "            \"VALUES\\n\"\n",
    "dictlist = data['Time Series (1min)'].items()\n",
    "sql_query = \"\"\n",
    "for dic in dictlist:\n",
    "    date, stocks_prices = dic\n",
    "    stocks_values = list(stocks_prices.values())\n",
    "    if not sql_query:\n",
    "        sql_query = \"    ('{:s}', {:s}, {:s}, {:s}, {:s}, {:s});\\n\".format(date, stocks_values[0], stocks_values[1], stocks_values[2], stocks_values[3], stocks_values[4]) + sql_query\n",
    "    else:\n",
    "        sql_query = \"    ('{:s}', {:s}, {:s}, {:s}, {:s}, {:s}),\\n\".format(date, stocks_values[0], stocks_values[1], stocks_values[2], stocks_values[3], stocks_values[4]) + sql_query\n",
    "sql_query = sql_head + sql_query\n",
    "\n",
    "file = open('sql_inter_query.sql', 'w')\n",
    "file.write(sql_query)\n",
    "file.close()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}